package models;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.StringTokenizer;

import models.TweetGrabber;

import play.db.jpa.Model;

public class SentFilter 
{
	private static HashMap<String, Integer> positiv = new HashMap<String, Integer>();
	private static HashMap<String, Integer> negativ = new HashMap<String, Integer>();
	private static HashMap<String, Integer> ratingList = new HashMap<String, Integer>();
	
	ArrayList<String> waitList = new ArrayList<String>();
	ArrayList<Tweet> tweets;
	
	static StringTokenizer st;
	
	
	public SentFilter() throws SQLException
	{
	}
	
	public ArrayList<Tweet> getSentiment(ArrayList<Tweet> t) throws IOException
	{
		this.tweets = t;
		int rating = 0;
		String token;
		
		for(int i = 0; i < tweets.size(); i++)
        {
        	this.st = new StringTokenizer(tweets.get(i).getTextSent());
        	
        	while(st.hasMoreTokens())
        	{
        		token = st.nextToken().replaceAll("[^a-zA-Z]", "");
        		if(token.length() > 3)
        		{
        			rating += checkSent(token, i);
        		}
        	}
        	
        	this.tweets.get(i).setRating(rating);
        	
        	if(rating != 0)
    		{
    			checkRatingList(rating, i);
    		}
        	
        	waitList.clear();
        	rating = 0;
        }
		
		return this.tweets;
	}
	
	public int checkSent(String word, int i)
	{
		if(this.positiv.get(word) != null)
		{
			tweets.get(i).ratingWords += (word+" "+positiv.get(word)+" | ");
			return positiv.get(word);
		}
		else if(this.negativ.get(word) != null)
		{
			tweets.get(i).ratingWords += (word+" "+negativ.get(word)+" | ");
			return negativ.get(word);
		}
		else
		{
			this.waitList.add(word);
			return 0;
		}
	}
	
	public void checkRatingList(int r, int i)
	{
		String word;
		int value;
		
		if(!waitList.isEmpty())
		{
			for(int j = 0; j < waitList.size(); j++)
			{
				word = waitList.get(j);
				
				if(ratingList.get(word) == null)
				{
					value = 0;
					ratingList.put(word, value);
				}
				else
				{
					value = ratingList.get(waitList.get(j));
				}
				
				if(r >= 3)
				{
					if(ratingList.get(waitList.get(j)) < 100)
					{
						ratingList.put(word, (value+1));
						tweets.get(i).newWords += ("Increased: "+word+" "+(value+1)+" | ");
					}
					else
					{
						positiv.put(waitList.get(j), 1);
						ratingList.remove(waitList.get(j));
					}
				}
				else if(r <= -3)
				{					
					if(ratingList.get(waitList.get(j)) > -100)
					{
						ratingList.put(word, (value-1));
						tweets.get(i).newWords += ("Decreased: "+word+" "+(value-1)+" | ");
					}
					else
					{
						negativ.put(waitList.get(j), -1);
						ratingList.remove(waitList.get(j));
					}
				}
			}
		}
	}
	
	public static void writeSQL()
	{
		SQLwork.setPositiv(positiv);
		SQLwork.setNegativ(negativ);
		SQLwork.setRating(ratingList);
	}
	
	
	public static void readSQL() throws SQLException
	{
		negativ.clear();
		positiv.clear();
		ratingList.clear();
		
		positiv = SQLwork.getPositiv();
		negativ = SQLwork.getNegativ();
		ratingList = SQLwork.getRating();
	}
	
	public void read()
	{
		try 
		{
			BufferedReader br;
			
			String line = "";
			String word = "";
			String value = "";
			
			positiv.clear();
			
			br = new BufferedReader(new FileReader("Tweetalyze/app/positiv.txt"));
			while ((line = br.readLine()) != null) 
			{
				st = new StringTokenizer(line);
        		word = st.nextToken();
        		value = st.nextToken();
        		positiv.put(word, Integer.parseInt(value));
			}
			br.close();
			
			negativ.clear();
			
			br = new BufferedReader(new FileReader("Tweetalyze/app/negativ.txt"));
			while ((line = br.readLine()) != null) 
			{
				st = new StringTokenizer(line);
				word = st.nextToken();
				value = st.nextToken();
	        	negativ.put(word, Integer.parseInt(value));
			}
			br.close();
			
			ratingList.clear();
			
			br = new BufferedReader(new FileReader("Tweetalyze/app/ratingList.txt"));
			while ((line = br.readLine()) != null) 
			{
				st = new StringTokenizer(line);
				word = st.nextToken();
				value = st.nextToken();
	        	ratingList.put(word, Integer.parseInt(value));
			}
			br.close();
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
	
	public void write() throws IOException
	{
        BufferedWriter out; 
        
        out = new BufferedWriter(new FileWriter("Tweetalyze/app/positiv.txt"));

        for (String elem : positiv.keySet())
        {
        	out.write(elem +" "+ positiv.get(elem));
        	out.newLine();
        }
        out.close();
        
        out = new BufferedWriter(new FileWriter("Tweetalyze/app/negativ.txt"));

        for (String elem : negativ.keySet())
        {
        	out.write(elem +" "+ negativ.get(elem));
        	out.newLine();
        }
        out.close();
        
        out = new BufferedWriter(new FileWriter("Tweetalyze/app/ratingList.txt"));

        for (String elem : ratingList.keySet())
        {
        	out.write(elem +" "+ ratingList.get(elem));
        	out.newLine();
        }
        out.close();
	}
}
